jQuery(function() {
	
	
	// 评价按钮
	jQuery('.accessBtn').on('click',function() {
				var selects = getSelect("#accessTable", 1);
				if (selects.length > 1) {
					$.springLemonDialog({
						content : '一次只能评价一条记录',// 确认文本
						clickButton : function(sender, modal, index) {// 按钮事件处理
							$(this).closeDialog(modal);
						}
					});
				} else if (selects.length == 0) {
					$.springLemonDialog({
						content : '请选择要评价的记录',// 确认文本
						clickButton : function(sender, modal, index) {// 按钮事件处理
							$(this).closeDialog(modal);
						}
					});
				} else if (selects.length == 1) {
					if (selects[0].isSubmit==6) {
						$.springLemonDialog({
							content : '服务单已完成不能评价',// 确认文本
							clickButton : function(sender, modal, index) {// 按钮事件处理
								$(this).closeDialog(modal);
							}
						});
					} else {
						var id = selects[0].id;
						jQuery("#assessId").val(id);
						jQuery("#ticketName").val(selects[0].unitName);
						jQuery("#manageCode").val(selects[0].manageCode);
						jQuery("#assessMemo").val(selects[0].assessMemo);
						jQuery('#assessModal').modal('show');
					}
				}
			})
	// 保存评价
	jQuery(".saveBtn").on('click',function() {
				var id = jQuery("#assessId").val();
				var d = {
					"assess" : jQuery("#assess").val(),
					"assessMemo" : jQuery("#assessMemo").val()
				}
				var url = window.rootPath + '/api/afterSaleTickets/' + id;
				jQuery.ajax({
					url : encodeURI(url),
					type : 'patch',
					data : JSON.stringify(d),
					dataType : 'json',
					contentType : 'application/json;charset=utf-8',
					success : function() {
						var operateType = "售后服务回访";
						var content = "对售后服务单【" + jQuery("#ticketName").val()
								+ "】进行回访评价";
						addOperateLog(operateType, content)
						jQuery('#assessModal').modal('hide');
						serverTable.draw(false);
					}
				});
			});
	// table
	//获取选中状态
	$(function(){
		  $(":radio").click(function(){
			  //jQuery('#keyword').val();
			  serverData.key = jQuery('#keyword').val();
			  var isSubmit=$('input:radio[name="isReceived"]:checked').val();
			  serverData.isSubmit = isSubmit;
			  serverTable.draw();
		  });
	});
	var serverData = {};
	var serverTable;
	var isSubmit= $('input:radio[name="isReceived"]:checked').val();
	serverData.isSubmit=isSubmit;
	serverData.key = jQuery('#keyword').val();;
	jQuery(document).ready(function() {
						serverTable = jQuery('#accessTable')
								.DataTable(
										{
											"serverSide" : true,
											"ordering" : false,
											"ajax" : {
												"dataType" : "json",
												"type" : "get",
												"cache" : false,
												"url" : window.rootPath
														+ '/ui/afterSaleAssess/getpage',
												"data" : function(d) {
													return $.extend({}, d,
															serverData);
												}
											},
											columns : [
													{
														data : null,
														defaultContent : ""
													},
													{
														data : null,
														defaultContent : "",
														render : function(data,type, full,meta) {
															var operate = ' <input type="checkbox" class="checkchild" />';
															return operate;

														}
													},
													{
														data : null,
														render : function(data,type, full,meta) {
															var dOut;
															var isSubmit = data.isSubmit;
															if (isSubmit == "0") {
																dOut = '<td class="center" ><span class="badge label-pill label-danger">未提交</span></td>';
															} else if (isSubmit == "1") {
																dOut = '<td class="center" ><span class="badge label-pill label-primary">退回</span></td>';
															} else if (isSubmit == "2") {
																dOut = '<td class="center" ><span class="badge label-pill label-primary">已提交</span></td>';
															} else if (isSubmit == "3") {
																dOut = '<td class="center" ><span class="badge label-pill label-primary">已派遣</span></td>';
															} else if (isSubmit == "4") {
																dOut = '<td class="center" ><span class="badge label-pill label-primary">跟踪中</span></td>';
															} else if (isSubmit == "5") {
																dOut = '<td class="center" ><span class="badge label-pill label-primary">待回访</span></td>';
															} else if (isSubmit == "6") {
																dOut = '<td class="center" ><span class="badge label-pill label-success">已完成</span></td>';
															}
															return dOut;
														}
													},
													{
														data : 'manageCode'
													},
													{
														data : 'afterSaleType'
													},
													
													{
														data : 'serviceTime',
														render: function (data, type, full, meta) {
								                            if (data)
								                                return moment(data).format('YYYY/MM/DD');
								                            else
								                                return '';
								                     }
													},
													{
														data : 'superior'
													},
													{
														data : 'contractCode'
													},
													{
														data : 'linkMan'
													},
													{
														data : 'linkManPhone'
													}, 
													{
														data : 'assess'
													}, 
													{
														data : 'assessMemo',
										                render:function(data,type,full,meta){
										                	if(data == null || data == undefined || data == ""){
																return data;
															}else{
																return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
															}
														}
													} 
												],
											"drawCallback" : function(oSettings) {
												var k = 0;
												var total = oSettings.aiDisplay.length
														+ oSettings._iDisplayStart;
												for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
													$(
															'td:eq(0)',
															oSettings.aoData[oSettings.aiDisplay[k]].nTr)
															.text(i + 1);
													k++;
												}
											}
										});

					});
	// 完成服务单
	jQuery(".finishBtn").on('click',function() {
						var selects = getSelect("#accessTable", 1);
						if (selects.length == 0) {
							message("请选择要完成的记录");
						}
						else if(selects.length >1){
							message("一次只能完成一个服务单");
						}
						else if (selects.length == 1) {
							var submit=selects[0].isSubmit;
							if(submit!='5'){
								message('服务单未处理完，不能结束');
								return;
							}
							var assess=selects[0].assess;
							if(assess==null){
								message('服务单未评价，不能结束');
								return;
							}
							var unitName=selects[0].unitName;
							var id = selects[0].id;
							$.springLemonDialog({
								content : '确定完成' +unitName + "单位的服务单",// 确认文本
								clickButton : function(sender, modal,
												index) {// 按钮事件处理
											if (index == 0) {
												var url = window.rootPath+'/ui/afterSaleAssess/finishticket?id='+ id;
													jQuery.ajax({
														url:  encodeURI(url),
														cache:false,
											            type: 'get',
											            dataType: 'json',
											            success: function (data) {
											            	if(data.code==0){
											            		delTask(id,"售后服务单待回访")//删除待办任务
											            		var operateType = "售后服务回访";
																var content = "完成售后服务单【"+ unitName+ "】进行回访";
																addOperateLog(operateType,content)
																serverTable.draw(false);
											            	}	
											            }
											        });
											}
											$(this).closeDialog(modal);
										}
									});

						}

					});

	// 查询
	jQuery(".searchBtn").on('click', function() {
		serverData.key = jQuery("#keyword").val();
		var isSubmit=$('input:radio[name="isReceived"]:checked').val();
		serverData.isSubmit = isSubmit;
		serverTable.draw();
	});

	// 撤消
	jQuery(".cancelBtn").on('click', function() {
		jQuery("#keyword").val('');
		serverData.key = '';
		var isSubmit=$('input:radio[name="isReceived"]:checked').val();
		serverData.isSubmit = isSubmit;
		serverTable.draw();
	});

	// 列表全选
	$("#checkAll").click(function() {
		var check = $(this).prop("checked");
		$(".checkchild").prop("checked", check);
	});

});
